<template>
  <div class="article-comment">
    <!-- 全部评论 -->
    <!--
        粘性布局
     -->
    <van-sticky offset-top="11.73333vw">
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{article.comm_count}})</span>
        <span>0 点赞</span>
      </div>
    </van-sticky>
    <!-- 评论列表 -->
    <div class="list">
      <!-- Vant-list -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有评论了"
        @load="onLoad"
      >
        <div
          class="item van-hairline--bottom"
          v-for="comment in commentList"
          :key="comment.com_id"
        >
          <van-image
            round
            width="10vw"
            height="10vw"
            :src="comment.aut_photo"
          />
          <div class="info">
            <p>
              <span class="name">{{ comment.aut_name }}</span>
              <span class="zan"
                >{{ comment.like_count }} <geek-icon name="like2"
              /></span>
            </p>
            <p class="cont">{{ comment.content }}</p>
            <p>
              <span @click="gotoReply(comment.com_id, comment.aut_name)" class="reply"
                >{{ comment.reply_count }}条回复
                <i class="van-icon van-icon-arrow"></i
              ></span>
              <span class="time">2小时内</span>
            </p>
          </div>
        </div>
      </van-list>
    </div>
    <!-- 底部工具 -->
    <div class="footer">
      <div class="input" @click="gotoComment">
        <i class="van-icon van-icon-edit"></i>
      </div>
      <div class="btn" @click="changeComment">
        <geek-icon name="comment"></geek-icon>
        <p>评论</p>
        <i>{{ article.comm_count }}</i>
      </div>
      <div class="btn">
        <geek-icon
          @click.native="likeOrCancel"
          :name="article.attitude === 1 ? 'like-sel' : 'like'"
        ></geek-icon>
        <p>点赞</p>
      </div>
      <div class="btn">
        <geek-icon
          @click.native="collectArticle"
          :name="article.is_collected ? 'collect-sel' : 'collect'"
        ></geek-icon>
        <p>收藏</p>
      </div>
      <div class="btn">
        <geek-icon name="share"></geek-icon>
        <p>分享</p>
      </div>
    </div>

    <!-- 回复列表弹层 -->
    <van-popup v-model="reply.open" position="right">
      <!-- 头 -->
      <van-nav-bar
        left-arrow
        @click-left="reply.open = false;currentCommentId='';currentAuthor='';offsetCid=''"
        :title="reply.totalCount+'条回复'"
      />
      <div class="reply-wrapper list">
        <!-- 列表 -->
        <van-list :immediate-check="false"  v-model="reply.loading" :finished="reply.finished" finished-text="没有回复了" @load="loadReply">
          <div class="item van-hairline--bottom" v-for="item in reply.list" :key="item.com_id">
            <van-image
              round
              width="10vw"
              height="10vw"
              :src="item.aut_photo"
            />
            <div class="info">
              <p>
                <span class="name">{{item.aut_name}}</span>
                <span class="zan">{{item.like_count}} <geek-icon name="like2" /></span>
              </p>
              <p class="cont">{{item.content}}</p>
              <p><span class="time" style="margin-left: 0">{{item.pubdate | relativeTime}}</span></p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底 -->
      <div class="footer van-hairline--top" style="position: static">
        <div class="input big" @click="gotoComment"><i class="van-icon van-icon-edit"></i></div>
        <div class="btn">
          <geek-icon name="collect"></geek-icon>
          <p>收藏</p>
        </div>
        <div class="btn">
          <geek-icon name="share"></geek-icon>
          <p>分享</p>
        </div>
      </div>
    </van-popup>

     <!-- 评论&回复 -->
    <van-popup v-model="showInput" position="bottom">
      <van-nav-bar left-arrow @click-left="showInput=false" :title="title" right-text="发表" @ @click-right="submitComment"/>
      <van-field
        v-model.trim="text"
        rows="3"
        autosize
        type="textarea"
        maxlength="100"
        :placeholder="placeHodler"
        show-word-limit
      />
    </van-popup>
  </div>
</template>
<script>
import {
  getArticleComments,
  articleLikesOrCancel,
  collectArticle,
  getReplysByComment,
  commentOrReply
} from '@/api/article.js'
export default {
  props: ['article'],
  data () {
    return {
      text: '',
      showInput: false,
      loading: false,
      finished: false,
      offsetId: null,
      offsetCid: null,
      commentList: [],
      currentCommentId: '',
      currentAuthor: '',
      title: '',
      placeHodler: '',
      reply: {
        open: false,
        loading: false,
        finished: false,
        list: [],
        totalCount: 0
      }
    }
  },
  created () {
    // this.getCommentList()
  },
  methods: {
    //   显示评价窗口  -- 文本窗口
    gotoComment () {
      // 1.显示窗口
      this.showInput = true

      //   2.判断是否有评论ID
      if (this.currentCommentId) {
        this.title = '回复评论'
        this.placeHodler = '@' + this.currentAuthor
      } else {
        this.title = '评论文章'
        this.placeHodler = '请输入评价内容'
      }
    },
    //   发表一级评论
    async submitComment () {
      if (!this.currentCommentId) {
        if (!this.text) return this.$toast('请输入内容')
        const [err, data] = await commentOrReply(this.article.art_id, this.text, this.article.art_id)
        if (err) return this.$toast.fail(err)
        console.log(data)
        this.$toast.success('发表成功')
        //   1.退出评论框
        this.showInput = false
        // 2. 将评论的内容渲染到评论区 （重新加载一下评论不就行了）
        this.commentList = []
        this.onLoad()

        //   3.评论加1
        this.article.comm_count++
        //   4.清空text文本
        this.text = ''
      } else {
        //   二级评价
        if (!this.text) return this.$toast('请输入内容')
        const [err, data] = await commentOrReply(this.currentCommentId, this.text, this.article.art_id)
        if (err) return this.$toast.fail(err)
        console.log(data)
        // 1. 关闭回复窗口
        this.showInput = false
        // 2.清空text文本
        this.text = ''
        // 3.渲染二级回复列表数据
        this.offsetCid = ''
        this.loadReply(this.currentCommentId)

        // 4. 当前的二级评论数量加1
        this.commentList.forEach((item, index) => {
          if (item.com_id === this.currentCommentId) {
            this.commentList[index].reply_count++
          }
        })
      }
    },
    async loadReply (cid) {
      this.reply.loading = true
      // 加载数据
      //   2.请求数据
      const [err, data] = await getReplysByComment(cid, this.offsetCid)
      if (err) return this.$toast.fail(err)
      console.log(data)

      if (data.data.last_id === data.data.end_id) {
        // 回复加载完毕
        this.reply.finished = true
      }
      //   3.渲染
      this.reply.list = data.data.results
      this.reply.totalCount = data.data.total_count

      //   4.f赋值
      this.offsetCid = data.data.last_id

      //   5.加载完毕
      this.reply.loading = false
    },
    gotoReply (cid, name) {
      // 显示二级评论的窗口
      this.reply.open = true

      //   保存你点击的评论id
      this.currentCommentId = cid
      this.currentAuthor = name

      //   请求数据
      this.loadReply(cid)
    },
    //   点赞或者取消点赞
    async likeOrCancel () {
      if (this.article.attitude === 1) {
        await articleLikesOrCancel(this.article.art_id, false)
        //   执行成功 改变article的attitude
        this.article.attitude = -1
        this.$toast.success('取消点赞成功')
      } else {
        await articleLikesOrCancel(this.article.art_id, true)
        //   执行成功 改变article的attitude
        this.article.attitude = 1
        this.$toast.success('点赞成功')
      }
    },
    async collectArticle () {
      const [err] = await collectArticle(
        this.article.art_id,
        !this.article.is_collected
      )
      if (err) return this.$toast.fail('操作失败')
      this.article.is_collected = !this.article.is_collected
      this.$toast.success('操作成功')
    },
    changeComment () {
      this.$emit('changeComment')
    },
    // async getCommentList () {
    //   const [err, data] = await getArticleComments(this.article.art_id, this.offsetId)
    //   if (err) return this.$toast.fail(err)
    //   console.log(data)
    //   this.commentList = data.data.results
    //   //   将最后一个评论id赋值给offsetId
    //   this.offsetId = data.data.last_id
    // },
    async onLoad () {
      console.log('上拉加载')
      //   1. 先将loading 置为true
      this.loading = true
      // 2. 调用请求函数
      const [err, data] = await getArticleComments(
        this.article.art_id,
        this.offsetId
      )
      if (err) return this.$toast.fail(err)
      //  3.判断是否是最后一页
      if (data.data.last_id === data.data.end_id) {
        // 加载到了最后一条 没有更多数据了
        this.finished = true
      } else {
        this.offsetId = data.data.last_id
      }

      // 4.评论列表追加
      this.commentList.push(...data.data.results)

      // 5.将loading置为false
      this.loading = false
    }
  },
  name: 'ArticleComment'
}
</script>
<style scoped lang="less">
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    &.big {
      width: 260px;
    }
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    .van-icon {
      color: #999;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .geek-icon {
      font-size: 18px;
    }
    i {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .list {
    padding: 0 16px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan {
            font-size: 14px;
            float: right;
            color: #999;
            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}

.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.reply-wrapper {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

::v-deep .van-nav-bar__text {
  color: @geek-color;
}
::v-deep .van-field__control {
  background: @geek-gray-color;
  padding: 5px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
}
</style>
